home *** CD-ROM | disk | FTP | other *** search
/ Acorn RISC PD-CD 1 / Acorn RISC PD-CD 1.iso / utilities / _graphics / graphics / _jpeg / c / jrdppm < prev    next >
Encoding:
Text File  |  1991-10-28  |  3.0 KB  |  125 lines

  1. /*
  2.  * jrdppm.c
  3.  *
  4.  * Copyright (C) 1991, Thomas G. Lane.
  5.  * This file is part of the Independent JPEG Group's software.
  6.  * For conditions of distribution and use, see the accompanying README file.
  7.  *
  8.  * This file contains routines to read input images in PPM format.
  9.  * The PBMPLUS library is required (well, it will be in the real version).
  10.  *
  11.  * These routines may need modification for non-Unix environments or
  12.  * specialized applications.  As they stand, they assume input from
  13.  * an ordinary stdio stream.  They further assume that reading begins
  14.  * at the start of the file; input_init may need work if the
  15.  * user interface has already read some data (e.g., to determine that
  16.  * the file is indeed PPM format).
  17.  *
  18.  * These routines are invoked via the methods get_input_row
  19.  * and input_init/term.
  20.  */
  21.  
  22. #include "jinclude.h"
  23.  
  24. #ifdef PPM_SUPPORTED
  25.  
  26.  
  27. /*
  28.  * Read the file header; return image size and component count.
  29.  */
  30.  
  31. METHODDEF void
  32. input_init (compress_info_ptr cinfo)
  33. {
  34.   int c, w, h, prec;
  35.  
  36.   if (getc(cinfo->input_file) != 'P')
  37.     ERREXIT(cinfo->emethods, "Not a PPM file");
  38.  
  39.   c = getc(cinfo->input_file);
  40.   switch (c) {
  41.   case '5':                     /* it's a PGM file */
  42.     cinfo->input_components = 1;
  43.     cinfo->in_color_space = CS_GRAYSCALE;
  44.     break;
  45.  
  46.   case '6':                     /* it's a PPM file */
  47.     cinfo->input_components = 3;
  48.     cinfo->in_color_space = CS_RGB;
  49.     break;
  50.  
  51.   default:
  52.     ERREXIT(cinfo->emethods, "Not a PPM file");
  53.     break;
  54.   }
  55.  
  56.   if (fscanf(cinfo->input_file, " %d %d %d", &w, &h, &prec) != 3)
  57.     ERREXIT(cinfo->emethods, "Not a PPM file");
  58.  
  59.   if (getc(cinfo->input_file) != '\n' || w <= 0 || h <= 0 || prec != 255)
  60.     ERREXIT(cinfo->emethods, "Not a PPM file");
  61.  
  62.   cinfo->image_width = w;
  63.   cinfo->image_height = h;
  64.   cinfo->data_precision = 8;
  65. }
  66.  
  67.  
  68. /*
  69.  * Read one row of pixels.
  70.  */
  71.  
  72. METHODDEF void
  73. get_input_row (compress_info_ptr cinfo, JSAMPARRAY pixel_row)
  74. {
  75.   register FILE * infile = cinfo->input_file;
  76.   register JSAMPROW ptr0, ptr1, ptr2;
  77.   register long col;
  78.   
  79.   if (cinfo->input_components == 1) {
  80.     ptr0 = pixel_row[0];
  81.     for (col = cinfo->image_width; col > 0; col--) {
  82.       *ptr0++ = getc(infile);
  83.     }
  84.   } else {
  85.     ptr0 = pixel_row[0];
  86.     ptr1 = pixel_row[1];
  87.     ptr2 = pixel_row[2];
  88.     for (col = cinfo->image_width; col > 0; col--) {
  89.       *ptr0++ = getc(infile);
  90.       *ptr1++ = getc(infile);
  91.       *ptr2++ = getc(infile);
  92.     }
  93.   }
  94. }
  95.  
  96.  
  97. /*
  98.  * Finish up at the end of the file.
  99.  */
  100.  
  101. METHODDEF void
  102. input_term (compress_info_ptr cinfo)
  103. {
  104.   /* no work required */
  105. }
  106.  
  107.  
  108. /*
  109.  * The method selection routine for PPM format input.
  110.  * Note that this must be called by the user interface before calling
  111.  * jpeg_compress.  If multiple input formats are supported, the
  112.  * user interface is responsible for discovering the file format and
  113.  * calling the appropriate method selection routine.
  114.  */
  115.  
  116. GLOBAL void
  117. jselrppm (compress_info_ptr cinfo)
  118. {
  119.   cinfo->methods->input_init = input_init;
  120.   cinfo->methods->get_input_row = get_input_row;
  121.   cinfo->methods->input_term = input_term;
  122. }
  123.  
  124. #endif /* PPM_SUPPORTED */
  125.